package com.cssw.fyzb.web.service;

import com.alibaba.fastjson.JSON;
import com.cssw.fyzb.base.web.context.UserContext;
import com.cssw.fyzb.base.web.domain.AjaxResult;
import com.cssw.fyzb.core.dao.system.RoleDao;
import com.cssw.fyzb.core.model.dodb.SysFunc;
import com.cssw.fyzb.core.model.dodb.SysRole;
import com.cssw.fyzb.core.model.dodb.SysRoleFunc;
import com.cssw.fyzb.web.util.TreeUtil;
import com.cssw.fyzb.web.vo.FuncTreeNodeVo;
import com.cssw.fyzb.web.vo.SysFuncTree;
import com.ydn.dbframe.plugin.activerecord.Db;
import com.ydn.dbframe.plugin.activerecord.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Author yangyiqun
 * @create 2020-08-10 11:15
 */
@Service
public class RoleServiceImpl implements RoleService {

    @Autowired
    private RoleDao roleDao;

    @Override
    public AjaxResult list(String roleName, int page, int limit) {
        Page<SysRole> rolePage = roleDao.list(roleName, page, limit);

        return AjaxResult.success(rolePage.getList(), rolePage.getTotalRow());
    }

    @Override
    public AjaxResult save(Long roleId, String roleName, String roleDesc) {
        if (null != roleId) {
            roleDao.update(roleId, roleName, roleDesc, UserContext.getUserId());
        } else {
            roleDao.save(roleName, roleDesc, UserContext.getUserId());
        }

        return AjaxResult.success();
    }

    @Override
    public AjaxResult delete(Long roleId) {
        roleDao.delete(roleId);
        return AjaxResult.success();
    }


    @Override
    public AjaxResult saveRoleFunc(Long roleId, String funcList) {
        List<SysRoleFunc> roleFuncs = JSON.parseArray(funcList, SysRoleFunc.class);
        Db.tx(() -> {
            roleDao.deleteRoleFuncByRoleId(roleId,UserContext.getUserId());
            if (null != roleFuncs && roleFuncs.size() > 0) {
                for (SysRoleFunc roleFunc : roleFuncs) {
                    roleFunc.setRoleId(roleId);
//                    roleFunc.setCreateOperId(OperContext.getOperId());
                    roleFunc.setCreateTime(new Date());
                }

                roleDao.batchSaveRoleFunc(roleFuncs);
            }
            return true;
        });

        return AjaxResult.success();
    }

    @Override
    public AjaxResult querySysFuncTreeByRoleId(Long roleId) {
//        List<DictType> dictTypeList = dictDao.getDictTypeByCode(Dict.CMS_SYS_CODE);

        List<SysFuncTree> h_sysFuncTrees = new ArrayList<>();
        SysFuncTree h_sysFuncTree = null;
        List<SysFunc> funcList = null;
        List<FuncTreeNodeVo> h_funcTrees = null;

//        for (DictType dictType : dictTypeList) {
        h_sysFuncTree = new SysFuncTree();
//            h_sysFuncTree.setSysName(dictType.getTypeLabel());
//            h_sysFuncTree.setSysCode(dictType.getTypeValue());

        funcList = roleDao.queryFuncByRoleId(roleId);

        h_funcTrees = new ArrayList<>();
        FuncTreeNodeVo h_funcTree = null;
        for (SysFunc func : funcList) {
            h_funcTree = new FuncTreeNodeVo();

            h_funcTree.setFuncCode(func.getFuncCode());
            h_funcTree.setFuncIcon(func.getFuncIcon());
            h_funcTree.setName(func.getFuncName());
            if (null != func.getSupFuncId()) {
                h_funcTree.setpId(func.getSupFuncId().toString());
            }
            h_funcTree.setpId(func.getSupFuncId().toString());
            h_funcTree.setFuncId(func.getFuncId());
            h_funcTree.setFuncName(func.getFuncName());
            h_funcTree.setFuncType(func.getFuncType());
            h_funcTree.setFuncUrl(func.getFuncUrl());
            h_funcTree.setpFuncId(func.getFuncId());
            h_funcTree.setFuncIcon(func.getFuncIcon());
            h_funcTree.setSort(func.getSort());

            h_funcTree.setChecked(func.getInt("cfgCnt") > 0);

            h_funcTree.setId(func.getFuncId().toString());
            h_funcTree.setName(h_funcTree.getFuncName());

            h_funcTrees.add(h_funcTree);
        }
        h_sysFuncTree.setFuncTree(TreeUtil.build(h_funcTrees, "0"));
        h_sysFuncTrees.add(h_sysFuncTree);
//        }

        return AjaxResult.success(h_sysFuncTrees);
    }
}
